package com.ruoyi.web.controller.charge;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.charge.domain.ChargerGun;
import com.ruoyi.charge.service.IChargerGunService;
import com.ruoyi.common.constants.UserConstant;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.utils.SecurityUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.framework.annotation.RyDataScope;
import com.ruoyi.framework.annotation.RyLog;
import io.swagger.annotations.Api;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.Validated;

import java.util.Arrays;
import java.util.List;

/**
 * 充电枪 操作处理
 *
 * @author laifeng
 */
@Controller
@Mapping("charge/chargerGun")
@Api("充电枪管理")
public class ChargerGunController extends BaseController {

    @Inject
    IChargerGunService chargerGunService;

    /**
     * 查询充电枪列表
     */
//    @AuthPermissions("charge:gun:list")
    @Get
    @Mapping("list")
    @RyDataScope(deptAlias = UserConstant.DATA_SCOPE_ALIAS_DEPT, userAlias = UserConstant.DATA_SCOPE_ALIAS_USER)
    public PageData<ChargerGun> list(Page<ChargerGun> page, ChargerGun chargerGun) {
        QueryWrapper qw = getQW(chargerGun);
        Page<ChargerGun> result = chargerGunService.page(page, qw);
        return getPageData(result);
    }

    /**
     * 导出充电枪列表
     */
    @RyLog(title = "充电枪", businessType = BusinessType.EXPORT)
    @Post
    @Mapping("export")
    public void export(ChargerGun chargerGun) {
        QueryWrapper qw = getQW(chargerGun);
        List<ChargerGun> list = chargerGunService.list(qw);
        ExcelUtil.export(list);
    }

    /**
     * 查询充电枪详情
     */
    @Get
    @Mapping("{id}")
    public ChargerGun info(@Path Long id) {
        return chargerGunService.getById(id);
    }

    /**
     * 新增充电枪
     */
    @RyLog(title = "充电枪", businessType = BusinessType.INSERT)
    @Post
    @Mapping
    public Result<Void> add(@Body @Validated ChargerGun chargerGun) {
        return toResult(chargerGunService.addChargerGun(chargerGun));
    }

    /**
     * 修改充电枪
     */
    @RyLog(title = "充电枪", businessType = BusinessType.UPDATE)
    @Put
    @Mapping
    public Result<Void> edit(@Body @Validated ChargerGun chargerGun) {
        return toResult(chargerGunService.updateByIdChargerGun(chargerGun));
    }

    /**
     * 删除充电枪
     */
    @RyLog(title = "充电枪", businessType = BusinessType.DELETE)
    @Delete
    @Mapping("{ids}")
    public Result<Void> delete(@Path Long[] ids) {
        return toResult(chargerGunService.removeChargerGunsByIds(Arrays.asList(ids)));
    }

    private QueryWrapper getQW(ChargerGun chargerGun) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(ChargerGun::getChargerId).eq(chargerGun.getChargerId());
        qw.and(ChargerGun::getNumber).eq(chargerGun.getNumber());
        if (!SecurityUtil.isAdmin(SecurityUtil.getUserId())) {
            qw.and(ChargerGun::getCreateBy).eq(SecurityUtil.getUserName());
        }
        return qw;
    }

}
